<?php
namespace Home\Controller;

use Common\Controller\BaseController;
class RegisterController extends BaseController
{
    //注册
    public function index()
    {
        $this->display();
    }

    //保存信息
    public function save()
    {
        $obj = D('Member');
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $state = 'SUCCESS';
                $message = 'success';
                //启用邮箱认证
                if (C('EMAIL_VERIFY') == '1') {
                    $name = '';
                    $title = C('site_name') . ' member activation email';
                    $this->assign('verify', $data['verify']);
                    $content = $this->fetch('email');
                    //think_send_mail($data['username'], $name, $title, $content);
                    $url = 'http://api.kf01.com/email/mentoring.php';
                    $emailDate = array('email' => $data['username'], 'username' => $name, 'title' => $title, 'content' => $content);
                    $this->https_post($url, $emailDate);
                    $url = U('Register/success', array('email' => $data['username']));
                } else {
                    //更新验证码字段
                    cookie('userName', \Think\Crypt\Driver\Xxtea::encrypt($data['username'], C('cookie_fix')));
                    cookie('userID', \Think\Crypt\Driver\Xxtea::encrypt($list['userid'], C('cookie_fix')));
                    cookie('userType', \Think\Crypt\Driver\Xxtea::encrypt(0, C('cookie_fix')));  
                    $url = U('User/index/index');
                }
            } else {
                $state = 'operation failed';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state, $message, $url);
    }
    //跳转
    public function success()
    {
        $email = I('get.email');
        $emailDomain = 'mail.' . preg_replace('/^(.*)@(.*)/i', '$2', $email);
        switch ($emailDomain) {
            case 'mail.gmail.com':
                $emailDomain = 'www.gmail.com';
                break;
            case 'mail.hotmail.com':
                $emailDomain = 'www.hotmail.com';
                break;
            default:
                # code...
                break;
        }
        $this->assign('emailDomain', $emailDomain);
        $this->assign('email', $email);
        $this->display();
    }
    //重发邮件
    public function resend()
    {
        $email = I('post.email');
        if ($email == '') {
            echo 'email error';
            die;
        }
        $map['username'] = $email;
        $verify = M('Member')->where($map)->getField('verify');
        if ($verify == '') {
            echo 'Members are activated';
            die;
        }
        $name = '';
        $title = C('site_name') . ' member activation email';
        $this->assign('verify', $verify);
        $content = $this->fetch('email');
        //$this->think_send_mail($email, $name, $title, $content);
        $url = 'http://api.kf01.com/email/mentoring.php';
        $emailDate = array('email' => $email, 'username' => $name, 'title' => $title, 'content' => $content);
        $this->https_post($url, $emailDate);
        echo 'Mail has been sent';
    }
    //账号激活
    public function confirmation()
    {
        $code = $_GET['code'];
        $mebmer = M('Member');
        $list = $mebmer->where("verify='" . $code . "'")->find();
        if ($list == 0) {
            $this->error('Verify message is invalid');
        } else {
            $data['status'] = 1;
            $data['verify'] = '';
            $mebmer->where("verify='" . $code . "'")->save($data);
            // 根据条件保存修改的数据
            //更新验证码字段
            cookie('userName', \Think\Crypt\Driver\Xxtea::encrypt($list['username'], C('cookie_fix')));
            cookie('userID', \Think\Crypt\Driver\Xxtea::encrypt($list['userid'], C('cookie_fix')));
            cookie('userType', \Think\Crypt\Driver\Xxtea::encrypt($list['userType'], C('cookie_fix')));
            if ($list['userType'] == 1) {
                $url = U('Mentees/index/index');
            } elseif($list['userType'] == 2)  {
                $url = U('Mentors/index/index');
            }else{
                $url = U('User/index/index');
            }
            redirect($url);
        }
    }
}